package club.justwill.single.Service.impl;

import club.justwill.single.Service.InvitationService;
import club.justwill.single.bean.AddInvitation;
import club.justwill.single.bean.SearchInvitations;
import club.justwill.single.model.CoreConstants;
import club.justwill.single.model.JsonResponse;
import club.justwill.single.model.SecurityToken;
import club.justwill.single.model.SessionState;
import club.justwill.single.persistence.dao.InvitationDao;
import club.justwill.single.persistence.jpo.Invitation;
import club.justwill.single.util.JsonResponseHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * Created by luoyu on 15-12-27.
 */

@Service
@Scope(ConfigurableBeanFactory.SCOPE_SINGLETON)
public class InvitationServiceImpl extends BaseService implements InvitationService {
    @Autowired
    private InvitationDao invitationDao;

    @Override
    @Transactional(propagation = Propagation.REQUIRED, noRollbackFor = Throwable.class, readOnly = false)
    public void addInvitation(SecurityToken token, AddInvitation addInvitation, JsonResponse obj, SessionState sessionState) {
        //创建一个邀请
        Invitation invitation = new Invitation();
        invitation.setInviterId(addInvitation.getInviterId());
        invitation.setInviteeId(addInvitation.getInviteeId());
        invitation.setStatus(1);
        //加入数据库
        invitationDao.save(invitation, token);
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED, noRollbackFor = Throwable.class, readOnly = true)
    public List<Invitation> searchInvitations(SearchInvitations searchInvitations, JsonResponse obj) {
        List<Invitation> invitationList = invitationDao.searchInvitations(searchInvitations.getInviterId(),
                searchInvitations.getIndex(),searchInvitations.getSize());
        if (invitationList == null || invitationList.size() ==0) {
            JsonResponseHelper.addError(obj, CoreConstants.INTIVATION_IS_EMPTY);
        }
        return invitationList ;
    }


}
